package com.km.sglototo.dict.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.dict.entity.TbDictDetail;
import com.km.sglototo.dict.service.TbDictDetailService;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 数据字典详情 前端控制器
 * </p>
 *
 * @author xt
 * @since 2022-04-18
 */
@RestController
@RequestMapping("/sysDetail")
public class TbDictDetailController {
    @Resource
    TbDictDetailService tbDictDetailService;

    @SysLog(MODULE = "dict", REMARK = "字典查询")
    @ApiOperation("字典查询")
    @GetMapping(value = "/getAllId")
    public Object getList(TbDictDetail entity,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<TbDictDetail> queryWrapper = new QueryWrapper<>();
        if (entity != null) {
            queryWrapper.eq("delete_id", 0);
        }
        if (entity.getDictId() != null) {
            queryWrapper.eq("dict_id", entity.getDictId());
            queryWrapper.eq("delete_id", 0);
            queryWrapper.like("dict_id", entity.getDictId());
        }
        Page p = new Page<TbDictDetail>(pageNum, pageSize);
        IPage ipage = tbDictDetailService.page(p, queryWrapper);
        return new CommonResult().success(ipage);

    }


    @SysLog(MODULE = "dictDetail_add", REMARK = "新增字典详情")
    @ApiOperation("新增字典详情")
    @PostMapping(value = "/add")
    public Object addSysDict(@RequestBody TbDictDetail entity) {
        boolean b = false;
        if (entity.getDetailId() != null) {
            b = tbDictDetailService.updateById(entity);
        } else {
            b = tbDictDetailService.save(entity);
        }
        return new CommonResult().success(b);
    }

    @SysLog(MODULE = "Updata", REMARK = "更新字典信息")
    @ApiOperation("更新字典信息")
    @PostMapping(value = "/update/{id}")
    public Object updateInterview(@RequestBody TbDictDetail entity) {
        boolean b = tbDictDetailService.updateById(entity);
        return new CommonResult().success();
    }


    /*逻辑删除*/

    @GetMapping(value = "/delete/")
    public Object deleteUser(@RequestParam Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("用户id");
            }
            /*真删*/
            /*  QueryWrapper<Dict> qw = new QueryWrapper<>();
            qw.eq("dict_id",dictId);
            if (DictService.remove(qw)) {
                return new CommonResult().success();
            }*/

            TbDictDetail tbDictDetail = tbDictDetailService.getById(id);
            tbDictDetail.setDeleteId(1);
            /*逻辑删除*/
            if (tbDictDetailService.saveOrUpdate(tbDictDetail)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }
}




